package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.zjitc.mapper.BlogMapper;
import net.zjitc.mapper.UserMapper;
import net.zjitc.model.domain.*;
import net.zjitc.model.vo.BlogVO;
import net.zjitc.model.vo.MangerBlogVO;
import net.zjitc.service.AllbloguserService;
import net.zjitc.mapper.AllbloguserMapper;
import net.zjitc.service.BlogLikeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static net.zjitc.constants.SystemConstants.PAGE_SIZE;

/**
* @author Administrator
* @description 针对表【allbloguser(包括文章信息和用户信息，用于文章管理界面)】的数据库操作Service实现
* @createDate 2024-03-08 21:33:00
*/
@Service
public class AllbloguserServiceImpl extends ServiceImpl<AllbloguserMapper, Allbloguser>
    implements AllbloguserService{

    @Resource
    private AllbloguserMapper allbloguserMapper;

    @Resource
    private BlogLikeService blogLikeService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private BlogMapper blogMapper;

    @Value("${super.qiniu.url:null}")
    private String qiniuUrl;

    //文章管理界面，显示所有文章
    @Override
    public Page<MangerBlogVO> pageBlogManger(long currentPage, String title, Long userId) {
        LambdaQueryWrapper<Allbloguser> blogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogLambdaQueryWrapper.like(StringUtils.isNotBlank(title), Allbloguser::getTitle, title);
        blogLambdaQueryWrapper.orderBy(true, false, Allbloguser::getCreateTime);
        Page<Allbloguser> blogPage = this.page(new Page<>(currentPage, PAGE_SIZE), blogLambdaQueryWrapper);
        Page<MangerBlogVO> blogVoPage = new Page<>();
        BeanUtils.copyProperties(blogPage, blogVoPage);
        List<MangerBlogVO> blogVOList = blogPage.getRecords().stream().map((Allbloguser) -> {
            MangerBlogVO blogVO = new MangerBlogVO();
            BeanUtils.copyProperties(Allbloguser, blogVO);
            if (userId != null) {
                LambdaQueryWrapper<BlogLike> blogLikeLambdaQueryWrapper = new LambdaQueryWrapper<>();
                blogLikeLambdaQueryWrapper.eq(BlogLike::getBlogId, Allbloguser.getId()).eq(BlogLike::getUserId, userId);
                long count = blogLikeService.count(blogLikeLambdaQueryWrapper);
                blogVO.setIsLike(count > 0);
            }
            return blogVO;
        }).collect(Collectors.toList());
        for (MangerBlogVO blogVO : blogVOList) {
            String images = blogVO.getImages();
            if (images == null) {
                continue;
            }
            String[] imgStrs = images.split(",");
            blogVO.setCoverImage(qiniuUrl + imgStrs[0]);
        }
        blogVoPage.setRecords(blogVOList);
        return blogVoPage;
    }

//    @Override  可用的
//    public Page<Allbloguser> testselect(long currentPage, String title,Long userId) {
//            List<Allbloguser> blogList = allbloguserMapper.selectUserWithRole(title);
//            Page<Allbloguser> blogPage = new Page<>(currentPage, PAGE_SIZE);
//            int total = blogList.size();
//            blogPage.setTotal(total);
//            int fromIndex = (int) ((currentPage - 1) * PAGE_SIZE);
//            int toIndex = (int) Math.min(fromIndex + PAGE_SIZE, total);
//            blogPage.setRecords(blogList.subList(fromIndex, toIndex));
//            return blogPage;
//
//    }

//    @Override
//    public Page<Allbloguser> testselect(long currentPage, String title,Long userId) {
//
//            //查询全部的结果
//            List<Allbloguser> blogList = allbloguserMapper.selectUserWithRole(title);
//            Page<Allbloguser> blogPage = new Page<>(currentPage, PAGE_SIZE);
//            //根据查询完的结果对标题进行模糊查询
//            int total = blogList.size();
//            blogPage.setTotal(total);
//            int fromIndex = (int) ((currentPage - 1) * PAGE_SIZE);
//            int toIndex = (int) Math.min(fromIndex + PAGE_SIZE, total);
//            blogPage.setRecords(blogList.subList(fromIndex, toIndex));
//            return blogPage;
//
//    }

    @Override
    public Page<Allbloguser> testselect(long currentPage, String title, Long userId) {
        // 查询全部的结果
        List<Allbloguser> blogList = allbloguserMapper.selectUserWithRole(title);

        // 如果标题不为空,对查询结果进行标题模糊查询过滤
        if (StringUtils.isNotBlank(title)) {
            blogList = blogList.stream()
                    .filter(blog -> blog.getTitle().contains(title))
                    .collect(Collectors.toList());
        }

        Page<Allbloguser> blogPage = new Page<>(currentPage, PAGE_SIZE);
        int total = blogList.size();
        blogPage.setTotal(total);
        int fromIndex = (int) ((currentPage - 1) * PAGE_SIZE);
        int toIndex = (int) Math.min(fromIndex + PAGE_SIZE, total);
        blogPage.setRecords(blogList.subList(fromIndex, toIndex));
        return blogPage;
    }

}




